<template>
    <div>
        <router-link
          tag="div"
          to="/"
          class="header-abs"
          v-show="showAbs"
          >
            <div class="iconfont header-abs-back">&#xec73;</div>
        </router-link>
        <div
          class="header-fixed"
          v-show="!showAbs"
          :style="opacityStyle"
          >
            景点详情
        <router-link to="/">
            <div class="iconfont header-fixed-icon">&#xec73;</div>
        </router-link>
        </div>
    </div>
</template>
<script>
export default {
  name: 'DetailHeader',
  data () {
    return {
      showAbs: true,
      opacityStyle: {
        opacity: 0
      }
    }
  },
  methods: {
    handleScroll () {
      const top = document.documentElement.scrollTop
      // 滑动时的距离
      if (top > 60) {
        let opacity = top / 140
        opacity = opacity > 1 ? 1 : opacity
        this.opacityStyle = { opacity }
        this.showAbs = false
      } else {
        this.showAbs = true
      }
    }
  },
  activated () {
    // 在window上绑定事件，那么并不是是作用于当前这个组件 而是全局都会触发
    // 解绑事件
    window.addEventListener('scroll', this.handleScroll)
  },
  deactivated () {
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>
<style scoped>
.header-abs{
    position:absolute;
    left:.2rem;
    top:.2rem;
    width: .6rem;
    height: .6rem;
    line-height: .6rem;
    text-align: center;
    border-radius: .3rem;
    background:rgba(0, 0, 0, .4)
}
.header-abs-back{
    position: absolute;
    color:#fff;
    left:.1rem;
}
.header-fixed{
  position:fixed;
  left: 0;
  right: 0;
  top:0;
  height: .86rem;
  line-height: .86rem;
  background:#00bcd4;
  text-align: center;
  color:#fff;
  font-size: .36rem;
  z-index: 2;
}
.header-fixed-icon{
  position:absolute;
  left:0;
  top:0;
  /* padding-left:.2rem; */
  text-align: center;
  width:.64rem;
  font-size: .4rem;
  color:#fff;
}
</style>
